/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.biz.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.idgen.IdGen;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.biz.entity.BizGoods;
import com.jeesite.modules.biz.entity.BizOrder;
import com.jeesite.modules.biz.service.BizGoodsService;
import com.jeesite.modules.biz.service.BizOrderService;

/**
 * 订单表Controller
 * @author Lai_Hanzhang
 * @version 2019-03-13
 */
@Controller
@RequestMapping(value = "${adminPath}/biz/bizOrder")
public class BizOrderController extends BaseController {

	@Autowired
	private BizOrderService bizOrderService;
	
	@Autowired
	private BizGoodsService bizGoodsService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public BizOrder get(String id, boolean isNewRecord) {
		return bizOrderService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("biz:bizOrder:view")
	@RequestMapping(value = {"list", ""})
	public String list(BizOrder bizOrder, Model model) {
		model.addAttribute("bizOrder", bizOrder);
		return "modules/biz/bizOrderList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("biz:bizOrder:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<BizOrder> listData(BizOrder bizOrder, HttpServletRequest request, HttpServletResponse response) {
		bizOrder.setPage(new Page<>(request, response));
		Page<BizOrder> page = bizOrderService.findPage(bizOrder);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("biz:bizOrder:view")
	@RequestMapping(value = "form")
	public String form(BizOrder bizOrder, Model model) {
		model.addAttribute("bizOrder", bizOrder);
		if(StringUtils.isBlank(bizOrder.getId())){
		    String bizCode = IdGen.nextId();
		    bizOrder.setNumber(bizCode);
		}
		model.addAttribute("goodsList", bizGoodsService.getGoodsDown(new BizGoods()));
		return "modules/biz/bizOrderForm";
	}

	/**
	 * 保存订单表
	 */
	@RequiresPermissions("biz:bizOrder:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated BizOrder bizOrder) {
		bizOrderService.save(bizOrder);
		return renderResult(Global.TRUE, text("保存订单表成功！"));
	}
	
	/**
	 * 停用订单表
	 */
	@RequiresPermissions("biz:bizOrder:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(BizOrder bizOrder) {
		bizOrder.setStatus(BizOrder.STATUS_DISABLE);
		bizOrderService.updateStatus(bizOrder);
		return renderResult(Global.TRUE, text("停用订单表成功"));
	}
	
	/**
	 * 启用订单表
	 */
	@RequiresPermissions("biz:bizOrder:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(BizOrder bizOrder) {
		bizOrder.setStatus(BizOrder.STATUS_NORMAL);
		bizOrderService.updateStatus(bizOrder);
		return renderResult(Global.TRUE, text("启用订单表成功"));
	}
	
	/**
	 * 删除订单表
	 */
	@RequiresPermissions("biz:bizOrder:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(BizOrder bizOrder) {
		bizOrderService.delete(bizOrder);
		return renderResult(Global.TRUE, text("删除订单表成功！"));
	}
	
}